//导入mysql包
const mysql = require('mysql')

//创建连接池
const pool = mysql.createPool({
    host: '127.0.0.1', //连接该ip地址下计算机的MySQL
    user: 'root', //登录MySQL的账号
    password: 'admin123', //登录MySQL的密码
    database: 'fql', //设置要操作的数据库
    waitForConnections: true,
    connectionLimit: 10,
    queueLimit: 0
})

//测试连接
function testConnection() {
    return new Promise((resolve, reject) => {
        pool.getConnection((err, connection) => {
            if (err) {
                console.error('MySQL数据库连接失败:', err)
                reject(err)
                return
            }
            console.log('MySQL数据库连接成功，http服务已启动...')
            connection.release()
            resolve(true)
        })
    })
}

module.exports = {
    testConnection,
    pool
}
